package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

@Controller
@RequestMapping("/system/upload")
public class UploadController {
	
	//获取配置中的userfilepath的配置数据
	//private String userfilepath = RuoYiConfig.getUploadPath();
	private String userfilepath = "D:/userfiles/zjtt/qushiqueren";

	@RequestMapping("UploadFile")
	public ResponseEntity<?> UploadImage(HttpServletRequest request,
			HttpServletResponse response) {
		try {

			System.out.println(userfilepath);

			//去除userfilepath最后的斜杠
			if(userfilepath.endsWith("/")){
				userfilepath  = userfilepath.substring(0, userfilepath.length()-1);
			}
			//取出文件信息
			MultipartHttpServletRequest  mmRequest = (MultipartHttpServletRequest) request;

			//取出文件信息
			//MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
			//MultipartHttpServletRequest mmRequest = resolver.resolveMultipart(request);

			//取出文件信息
			//org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest mmRequest = (DefaultMultipartHttpServletRequest) request;
			Iterator<String> mm = mmRequest.getFileNames();
			String mmKeyString = mm.next();

			MultipartFile mmFile = mmRequest.getFile(mmKeyString);
			
			//取出文件大小
			int wenjiandaxiao = (int) mmFile.getSize();
			
			//获取上传文件的路径
			String mmFuJianPath = request.getParameter("fujianpath").toString();
			//获取上传文件限制的大小
			String mmFileSize = request.getParameter("filesize") == null ? "" : request.getParameter("filesize").toString();
			//转化为字节数比较
			int filesize = Integer.valueOf(mmFileSize) * 1024;
			//若上传文件的大小大于文件限制的大小，则返回错误，无法上传
			if(filesize < wenjiandaxiao) {
				return new ResponseEntity<String>("error_wenjiandaxiao", HttpStatus.FAILED_DEPENDENCY);
			}
			//拼接上传文件存放地址
			String SaveFolder = "";
			SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyyMMdd");
			SaveFolder = "/"+mmDateFormat.format(new Date()) + "/";
//			
//			ServletContext mmContext = request.getServletContext();
//			String FileUrl = mmContext.getContextPath() + SaveFolder;
//			System.out.println(FileUrl);
			
			String mmSavePath = userfilepath + SaveFolder;
			
			//判断文件夹时候存在，若不存在，则生成文件夹
			File mmFold = new File(mmSavePath);
			if (!mmFold.isDirectory() || !mmFold.exists()) {
				mmFold.mkdirs();
			}

			//取出文件类型，并生成新的文件名
			String mmExt = mmFile.getOriginalFilename().substring(
					mmFile.getOriginalFilename().lastIndexOf("."));
			String mmSaveName = UUID.randomUUID().toString() + mmExt;

			mmSavePath += mmSaveName;

			SaveFileFromInputStream(mmFile.getInputStream(), mmSavePath);
			return new ResponseEntity<String>(SaveFolder+mmSaveName, HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseEntity<String>(e.getMessage(), HttpStatus.OK);
		}

	}

	/*
	author:胡文祥
	2019-12-17
	 */
	//根据输入流和路径，生成新的文件
	public static void SaveFileFromInputStream(InputStream stream, String path)
			throws IOException {
		FileOutputStream fs = new FileOutputStream(path);
		byte[] buffer = new byte[1024 * 1024];
		int byteread = 0;
		while ((byteread = stream.read(buffer)) != -1) {

			fs.write(buffer, 0, byteread);
			fs.flush();
		}
		fs.close();
		stream.close();
	}
}